<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" class="form-inline">
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleAddPlatform">新增</el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-search" @click="handleSearchPlatformList">查询</el-button>
      </el-form>

      <el-table :data="platformList" border style="width: 100%" >
        <el-table-column prop="name" label="平台标识" width=""/>
        <el-table-column prop="describe" label="平台描述" width=""/>
        <el-table-column prop="department" label="所属大平台" width=""/>
        <el-table-column label="操作" align="center" width="200" >
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEditPlatform(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDeletePlatform(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :visible.sync="editFormVisible" :title="editFormTitle">
        <el-form ref="editForm" :model="tempPlatform" :rules="rules" label-position="left" label-width="100px">
          <el-form-item label="平台标识" prop="name">
            <el-input v-model="tempPlatform.name" placeholder="平台标识" style="width: 400px;"/>
          </el-form-item>
          <el-form-item label="平台描述" prop="describe">
            <el-input v-model="tempPlatform.describe" placeholder="平台描述" style="width: 400px;"/>
          </el-form-item>
          <el-form-item label="所属大部门" prop="department">
            <el-select v-model="tempPlatform.department" placeholder="所属大平台" default-first-option filterable class="filter-item" style="width: 400px">
              <el-option v-for="item in departmentOptions" :key="item.key" :label="item.name" :value="item.key"/>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editFormVisible = false">取 消</el-button>
          <el-button v-loading.fullscreen.lock="fullscreenLoading" type="primary" @click="handleSavePlatform">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { searchPlatformList, savePlatform, deletePlatform } from '@/api/settings.js'
export default {
  name: 'Settings',
  directives: { waves },
  components: { },
  data() {
    return {
      fullscreenLoading: false,
      editFormVisible: false,
      editFormTitle: '',
      platformList: [],
      departmentOptions: [
        { key: 'puhui', name: '大普惠' },
        { key: 'caifu', name: '大财富' },
        { key: 'jichu', name: '基础服务' }
      ],
      tempPlatform: {
        name: '',
        describe: '',
        department: '',
        methodNameList: []
      },
      rules: {
        name: [
          { required: true, message: '请输入平台标识', trigger: 'blur' }
        ],
        describe: [
          { required: true, message: '请输入平台描述', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请选择大部门', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.handleSearchPlatformList()
  },
  methods: {
    handleSearchPlatformList() {
      searchPlatformList().then(response => {
        if (response.code === '0000') {
          this.platformList = response.data
        }
      })
    },
    handleEditPlatform(row) {
      this.editFormVisible = true
      this.editFormTitle = '编辑平台'
      this.tempPlatform.name = row.name
      this.tempPlatform.describe = row.describe
      this.tempPlatform.department = row.department
    },
    handleAddPlatform() {
      this.editFormVisible = true
      this.editFormTitle = '新增平台'
      this.tempPlatform = {
        name: '',
        describe: '',
        department: '',
        methodNameList: []
      }
    },
    handleDeletePlatform(row) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tempPlatform.name = row.name
        this.fullscreenLoading = true
        deletePlatform(this.tempPlatform).then(response => {
          if (response.code === '0000') {
            this.$alert('删除平台成功!', '', {
              confirmButtonText: '确定',
              callback: action => {
                this.handleSearchPlatformList()
              }
            })
          } else {
            this.$message.error('error:' + response.msg)
          }
          this.fullscreenLoading = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSavePlatform() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.fullscreenLoading = true
          savePlatform(this.tempPlatform).then(response => {
            if (response.code === '0000') {
              this.$alert('保存平台成功!', '', {
                confirmButtonText: '确定',
                callback: action => {
                  this.handleSearchPlatformList()
                }
              })
              this.editFormVisible = false
            } else {
              this.$message.error('error:' + response.msg)
            }
            this.fullscreenLoading = false
          })
        }
      })
    }
  }
}
</script>
<style>
</style>

